package com.hrzyit.whistle.cases.widgets

import android.content.Context
import android.util.Log
import com.hrzyit.base.ext.onClick
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.data.bean.CaseIndexDeptInfoVo
import com.hrzyit.whistle.cases.event.SelectDeptAndUserEvent
import com.hrzyit.whistle.cases.presenter.WhistleSelectDeptPresenter
import com.lxj.xpopup.animator.PopupAnimator
import com.lxj.xpopup.core.CenterPopupView
import kotlinx.android.synthetic.main.popup_case_whistle_confirm.view.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 弹出窗口：案件吹哨确认界面
 * @author YangWei
 * @date 2019/4/21
 */
class CaseWhistleConfirmPopup (context: Context, val presenter: WhistleSelectDeptPresenter): CenterPopupView(context) {

    lateinit var caseIndexDeptInfoVoList: MutableList<CaseIndexDeptInfoVo>

    // 返回自定义弹窗的布局
    override fun getImplLayoutId(): Int {
        return R.layout.popup_case_whistle_confirm
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    override fun onCreate() {
        super.onCreate()

        initView()
    }

    /**
     * 选择单位-人员回调
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onSelectDeptAndUserEvent(event: SelectDeptAndUserEvent) {
        when (event.deptIdList.isEmpty()) {
            false -> {
                // 1. 更新单位名称列表
                mReceiverDeptNamesTv.text = event.deptNameList
//                authMsgEntityVo.receiveDeptNames = event.deptNameList
                // 2. 更新人员名称列表
                mReceiverUserNamesTv.text = event.userNameList
//                authMsgEntityVo.receiveUserNames = event.userNameList
                // 3. 更新单位id列表
//                authMsgEntityVo.receiveDeptIds = event.deptIdList
                // 4. 更新人员id列表
//                authMsgEntityVo.receiveUserIds = event.userIdList
                // 5.
//                authMsgEntityVo.userDeptInfoVoList = event.userDeptInfoVoList
            }
        }
    }


    /**
     * 初始化视图
     */
    private fun initView(){
        var deptNames = mutableSetOf<String>()
        var userNames = mutableSetOf<String>()

        caseIndexDeptInfoVoList.forEach {
            it.caseIndexUserInfoVoList?.forEach { user ->
                userNames.add(user.name)
                deptNames.add(user.deptName)
            }
        }
        // 设置发送到单位
        mReceiverDeptNamesTv.text = deptNames.joinToString("、")
        // 设置接受人列表
        mReceiverUserNamesTv.text = userNames.joinToString("、")

//        EventBus.getDefault().register(this)

        mConfirmBtn.onClick{
            // 吹哨内容描述
            mCaseWhistleContextEt?.let {
                Log.e("吹哨内容描述","${it.text}")
            }
//            EventBus.getDefault().unregister(this)
        }

        // 取消吹哨
        mCancelBtn.onClick {
            dismiss() // 关闭弹窗
//            EventBus.getDefault().unregister(this)
        }

    }

    // 设置最大宽度，看需要而定
    override fun getMaxWidth(): Int {
        return super.getMaxWidth()
    }

    // 设置最大高度，看需要而定
    override fun getMaxHeight(): Int {
        return super.getMaxHeight()
    }

    // 设置自定义动画器，看需要而定
    override fun getPopupAnimator(): PopupAnimator {
        return super.getPopupAnimator()
    }
}